package com.example.jf.jfapp.utils;

import android.content.Context;

import com.example.jf.jfapp.bean.LoginResponse;

/**
 * 位置：
 * 作用：
 * 时间：2017/7/26
 */

public class AppConfigUtils {
    static String ISLOGIN = "isLogin";

    private AppConfigUtils() {
    }

    private static AppConfigUtils mConfigUtils;

    public static final AppConfigUtils getInstance() {
        if (mConfigUtils == null) {
            mConfigUtils = new AppConfigUtils();
        }
        return mConfigUtils;
    }


    public boolean getLoginStatu(Context context) {
        return SpUtils.getBoolean(context, ISLOGIN);
    }

    public void setLoginStatu(Context context, boolean guide) {
        SpUtils.putBoolean(context, ISLOGIN, guide);
    }

    static class UserInfo {
        public static final String sessionToken = "sessionToken";
        public static final String updatedAt = "updatedAt";
        public static final String objectId = "objectId";
        public static final String username = "username";
        public static final String createdAt = "createdAt";
        public static final String headProtrait = "headProtrait";
        public static final String emailVerified = "emailVerified";
        public static final String mobilePhoneVerified = "mobilePhoneVerified";

    }

    /**
     * 保存用户信息
     * @param context
     * @param loginResponse
     */
    public void saveLoginInfo(Context context, LoginResponse loginResponse) {
        SpUtils.putString(context, UserInfo.sessionToken, loginResponse.getSessionToken());
        SpUtils.putString(context, UserInfo.updatedAt, loginResponse.getUpdatedAt());
        SpUtils.putString(context, UserInfo.objectId, loginResponse.getObjectId());
        SpUtils.putString(context, UserInfo.username, loginResponse.getUsername());
        SpUtils.putString(context, UserInfo.createdAt, loginResponse.getCreatedAt());


    }

    static LoginResponse loginResponse;

    /**
     * 获取用户信息
     * @param context
     * @return
     */
    public LoginResponse getLoginInfo(Context context) {
        if (loginResponse == null) {
            loginResponse = new LoginResponse();
            loginResponse.setSessionToken(SpUtils.getString(context, UserInfo.sessionToken));
            loginResponse.setUpdatedAt(SpUtils.getString(context, UserInfo.updatedAt));
            loginResponse.setObjectId(SpUtils.getString(context, UserInfo.objectId));
            loginResponse.setUsername(SpUtils.getString(context, UserInfo.username));
            loginResponse.setCreatedAt(SpUtils.getString(context, UserInfo.createdAt));
        }
        return loginResponse;
    }

    public void updateLoginInfo(Context context){
        if(loginResponse == null){
            loginResponse = new LoginResponse();
        }
        loginResponse.setSessionToken(SpUtils.getString(context, UserInfo.sessionToken));
        loginResponse.setUpdatedAt(SpUtils.getString(context, UserInfo.updatedAt));
        loginResponse.setObjectId(SpUtils.getString(context, UserInfo.objectId));
        loginResponse.setUsername(SpUtils.getString(context, UserInfo.username));
        loginResponse.setCreatedAt(SpUtils.getString(context, UserInfo.createdAt));
    }




    /**
     * 清除所有数据
     * @param context
     * @return
     */
    public boolean clearLoginInfo(Context context) {
        mConfigUtils = null;
        return SpUtils.clearData(context);
    }



}
